define(['jquery', 'bootstrap', 'backend', 'table', 'form','upload',], function ($, undefined, Backend, Table, Form, Upload) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'filedata/index',
                    add_url: 'filedata/add',
                    edit_url: 'filedata/edit',
                    del_url: 'filedata/del',
                    multi_url: 'filedata/multi',
                    importfiledata_url: 'filedata/importfiledata',
                    table: 'filedata',
                }
            });

            var table = $("#table");



            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        // {field: 'admin_id', title: __('Admin_id')},
                        {field: 'acount', title: __('Acount')},
                        {field: 'instumentname', title: __('Instumentname')},
                        {field: 'instumenttype', title: __('Instumenttype')},
                        {field: 'instumentsno', title: __('Instumentsno')},
                        {field: 'projectname', title: __('Projectname')},
                        {field: 'filenum', title: __('Filenum')},
                        {field: 'filecapacity', title: __('Filecapacity'), formatter: Table.api.formatter.fsizeformat},
                        {field: 'datasource', title: __('Datasource')},
                        {field: 'datafiles', title: __('Datafiles'), formatter: Table.api.formatter.commadataformat},
                        //{field: 'refreshtime', title: __('Refreshtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.date},
                        //{field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'field1', title: __('Field1')},
                        // {field: 'field2', title: __('Field2')},
                        // {field: 'field3', title: __('Field3')}, commadataformat
                        // {field: 'field4', title: __('Field4')},
                        // {field: 'field5', title: __('Field5')},
                        {field: 'weigh', title: __('Weigh')},
                        {field: 'status', title: __('Status'), visible:false, searchList: {"normal":__('normal'),"hidden":__('hidden')}},
                        {field: 'status_text', title: __('Status'), operate:false},
                        {field: 'state', title: __('State'), visible:false, searchList: {"state 0":__('State 0'),"state 1":__('State 1')}},
                        {field: 'state_text', title: __('State'), operate:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, buttons: [{
                                    name: 'importfiledata',
                                    text: __('Multi Import'),
                                    icon: 'fa fa-upload',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'filedata/importfiledata'
                                },
                                {
                                    name: 'exportfiledata',
                                    text: __('Multi Export'),
                                    icon: 'fa fa-download',
                                    classname: 'btn btn-info btn-xs btn-detail',
                                    url: 'filedata/exportfiledata'
                                }],
                                formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        importfiledata: function(){
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () { 
                // 仪器名称
                $(document).on("change", "#c-instumentname", function () {

                    var iname = $(this).val();

                    // 清空仪器类型下拉数据
                    $('#c-instumenttype').empty(); 
                    
                    // 初始化仪器类型选择数据
                    var option = $("<option>").val('').text(__('Please Choice Instument Style')); 
                    $("#c-instumenttype").append(option); 

                    if(iname) {                    
                        $.get("/admin/ajax/instype?iname="+iname, function(data, status){
                            data =  $.parseJSON( data );
                            if(data.length) {                             
                                $.each(data, function(i, row){
                                    var option = $("<option>").val(row.name).text(row.name); 
                                    $("#c-instumenttype").append(option);  
                                });                                                       
                            }
                         });

                     //$(this).prop('disabled',true);
                    } 
                });


                Form.api.bindevent($("form[role=form]"));
                Form.api.bindevent($("form[role=upload-form]"))
            }
        }
    };
    return Controller;
});

